package com.service;

import java.sql.SQLException;
import java.util.List;

import com.model.User;
import com.repository.UserRepositoryImpl;

public class UserServiceImpl implements UserService {

	UserRepositoryImpl userRep;
	List<User> userlist;

	public UserServiceImpl() throws SQLException {
		this.userRep = new UserRepositoryImpl();
	}

	// 用户注册
	public boolean userRegister(User user) {
		if (userRep.getOneUser(user) == null) {
			if (userRep.addUser(user) == 1) {
				return true;
			}
			System.out.println("注册失败");
		}
		return false;
	}

	// 用户名AJAX验证
	public boolean userNameAJAX(String username) {
		userlist = userRep.getAllUser();

		for (User ulist : userlist) {
			if (ulist.getUsername().equals(username)) {
				return true;
			}
		}
		return false;
	}

	// 用户登录
	public boolean userLogin(User user) {
		if(userRep.getOneUser(user)!=null){
			if (userRep.getOneUser(user).getUsername().equals(user.getUsername())
					&& userRep.getOneUser(user).getUsername().equals(user.getPassword())) {
				return true;
			}
		}
		return false;
	}

}
